package com.example.homework.h2021_04_16;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.homework.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private DataAdapter dataAdapter;
    private List<String> datas;
    private Button btnAdd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvList = findViewById(R.id.rv_list);
        btnAdd = findViewById(R.id.btn_add);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        rvList.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        datas = new ArrayList<>();
        for (int i = 0; i < 21; i++) {
            datas.add("数据"+i);
        }
        dataAdapter = new DataAdapter();
        rvList.setAdapter(dataAdapter);
        btnAdd.setOnClickListener(v->{
            Random random = new Random();
            int pos = random.nextInt(datas.size()-1);
            rvList.smoothScrollToPosition(pos);
            datas.add(pos,"添加的数据"+Math.random());
            dataAdapter.notifyItemInserted(pos);

        });
    }

    private NotificationManager notificationManager;
    private String channelId = "notification1";
    private void notifyChanged(String message){
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            Notification notification = new NotificationCompat.Builder(this,channelId)
                    .setContentText(message)
                    .setContentTitle("这里是标题")
                    .setSmallIcon(R.drawable.ic_app)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_app))
                    .build();
            //创建通知渠道
            NotificationChannel channel = new NotificationChannel(channelId,"测试通知", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(100,notification);
        }else {
            Notification notification =
                    new NotificationCompat.Builder(this)
                            .setContentText(message)
                            .setContentTitle("这里是标题")
                            .setSmallIcon(R.drawable.ic_app)
                            .build();
            notificationManager.notify(100,notification);
        }
    }

    class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_data,parent,false);
            return new DataViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((DataViewHolder)holder).tvData.setText(datas.get(position));
            holder.itemView.setOnLongClickListener(v->{
                boolean isOpen = NotificationUtils.isNotificationEnabled(MainActivity.this);
                if (isOpen) {
                    new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("是否要删除第" + position + "项数据？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            datas.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position, datas.size() - position);
                            notifyChanged("数据"+position+"已删除");
                        }
                    }).setNegativeButton("取消", null).show();
                }else {
                    NotificationUtils.gotoSet(MainActivity.this);
                }
                return false;
            });
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
    }



    class DataViewHolder extends RecyclerView.ViewHolder{

        public TextView tvData;

        public DataViewHolder(@NonNull View itemView) {
            super(itemView);
            tvData = itemView.findViewById(R.id.tv_data);
        }
    }
}